from django.contrib.auth import get_user_model
from django.db import models
from shortuuidfield import ShortUUIDField


class Type(models.Model):
    name = models.CharField(max_length=20, unique=True)

    class Meta:
        db_table = 'post_type'

    def __str__(self):
        return self.name


class Topic(models.Model):
    tid = ShortUUIDField(primary_key=True)
    title = models.CharField(max_length=100, unique=True)
    desc = models.CharField(max_length=400)
    pub_time = models.DateTimeField(auto_now_add=True)
    author = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)

    class Meta:
        db_table = 'post_topic'

    def __str__(self):
        return self.title


class Post(models.Model):
    pid = ShortUUIDField(primary_key=True)
    title = models.CharField(max_length=200)
    content = models.TextField()
    type = models.ForeignKey(Type, on_delete=models.SET_NULL,related_name='posts', null=True)
    topic = models.ForeignKey(Topic, on_delete=models.SET_NULL,related_name='posts', null=True)
    pub_time = models.DateTimeField(auto_now_add=True)
    author = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)

    class Meta:
        db_table = 'post'

    def __str__(self):
        return self.title
